package com.ivan.stu.notetool.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.github.zackratos.ultimatebar.UltimateBar;
import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.adapter.NoteAdapter;
import com.ivan.stu.notetool.callback.ItemClickListener;
import com.ivan.stu.notetool.callback.NegativeCallBack;
import com.ivan.stu.notetool.callback.PositiveCallBack;
import com.ivan.stu.notetool.model.NoteBean;
import com.ivan.stu.notetool.model.NotifyFinishEvent;
import com.ivan.stu.notetool.model.NotifyToRefreshNoteEvent;
import com.ivan.stu.notetool.model.OnSubjectChoosedEvent;
import com.ivan.stu.notetool.network.api.SafeApiManager;
import com.ivan.stu.notetool.network.intercepter.NetworkTransformer;
import com.ivan.stu.notetool.network.postbody.NoteInfoPostBody;
import com.ivan.stu.notetool.network.postbody.NotePostBody;
import com.ivan.stu.notetool.network.postbody.UpLoadNotePostBody;
import com.ivan.stu.notetool.network.response.ApiResponse;
import com.ivan.stu.notetool.network.response.NoteResponse;
import com.ivan.stu.notetool.network.response.OperateNoteResponse;
import com.ivan.stu.notetool.utils.ActivityCollectorUtils;
import com.ivan.stu.notetool.utils.ConstrantManager;
import com.ivan.stu.notetool.utils.DateUtils;
import com.ivan.stu.notetool.utils.EditTextUtil;
import com.ivan.stu.notetool.utils.FileManager;
import com.ivan.stu.notetool.utils.HtmlUtil;
import com.ivan.stu.notetool.utils.TextViewUtils;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.observers.ResourceObserver;


public class NoteActivity extends ThemeActivity implements View.OnClickListener{
    private static final int DOWNLOAD_COMPLETE = 0;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private FloatingActionButton bt_fab;
    private List<NoteBean> noteList = new ArrayList<NoteBean>();
    private NoteAdapter mAdapter ;
    private LinearLayoutManager llManager;
    private int totalCount = 0;
    private int pageSize = 10;
    private int pageIndex = 0;
    private int currentCount = 0;
    private boolean loading  = false;
    private boolean isRefreshing  = false;
    private String mCurrentSysID = "";
    private String mCurrentSysName = "";
    private String mCurrentSubjectID = "";
    private String mUserId = "";
    private int mUserType ;
    private String mUserName = "";
    private String mSchoolId = "";
    private String mToken = "";
    private ImageView iv_empty,iv_left_back;
    private TextView tv_empty;
    private ImageView iv_filter;
    private EditText et_search;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == DOWNLOAD_COMPLETE){



                if (mAdapter.mFooterHolder!=null){

                    Log.e("设置已加载完成","嘻嘻");
                    mAdapter.mFooterHolder.setData(2);
                }
                mSwipeRefreshLayout.setRefreshing(false);

            }
        }
    };
    private String mKeyword = "";
    private int IsKeyPoint = -1;
    private ImageView iv_highlight_star,iv_add_note;
    private RelativeLayout fl_search;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lgnotelib_activity_note);
        Utils.init(this);
        FileManager.init(NoteActivity.this,ConstrantManager.FILE_MANAGER_ROOT);
        UltimateBar.Companion.with(this).applyNavigation(false).create().transparentBar();
        if (!EventBus.getDefault().isRegistered(this)) {

            EventBus.getDefault().register(this);

        }
        setToolBar(NoteActivity.this);

        Intent intent = getIntent();
        mCurrentSysID = intent.getStringExtra("systemId");
        mUserId = intent.getStringExtra("userId");
        mUserType = intent.getIntExtra("userType",2);
        mSchoolId = intent.getStringExtra("schoolId");
        mToken = intent.getStringExtra("token");
        mUserName = intent.getStringExtra("userName");

        initSystemName();
      //  Log.e("systemid",mCurrentSysName+"打开了笔记工具");
        initView();
        upLoadNote();
        initData();
        initItemListener();
     //   ActivityCollectorUtils.addActivity(this);


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void notifyFinish(NotifyFinishEvent event){

        if (event.getFinishSigin() == 1){


            finish();
        }

    }


    public void upLoadNote(){

        UpLoadNotePostBody body = new UpLoadNotePostBody();
        body.setMaterialContent("");
        body.setMaterialID(ConstrantManager.MaterialID);
        body.setMaterialName(ConstrantManager.MaterialName);
        body.setMaterialTotal(ConstrantManager.MaterialTotal);
        body.setMaterialURL("");
        body.setResourceAndroidLink(ConstrantManager.ResourceAndroidLink);
        body.setResourcePCLink("");
        body.setResourceIOSLink("");
        body.setResourceID(ConstrantManager.currentResourceID);
        body.setResourceName(ConstrantManager.currentResourceName);
        body.setSystemType(3);


        SafeApiManager.getTestApi(3).upLoadNoteInfo(body)
                .compose(NetworkTransformer.<OperateNoteResponse>commonSchedulers())
                .subscribe(new ResourceObserver<OperateNoteResponse>() {
                    @Override
                    public void onNext(OperateNoteResponse operateNoteResponse) {

                        Log.e("aaaaaaaaa","上传成功！");

                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.e("aaaaaaaaa",e.toString()+"");

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNotifyToRefreshEvent(NotifyToRefreshNoteEvent event){

        if (event.getResult() == 1){

            //重置筛选条件

       //     mCurrentSysID = "All";
      //      mCurrentSubjectID = "All";
            mKeyword = "";
        //    ConstrantManager.currentSystemid = "All";
       //     ConstrantManager.currentSystemPostion = 0;
      //      ConstrantManager.currentSystemName = "全部";
      //      ConstrantManager.currentNoteSubjectPosition = 0;
          //  ConstrantManager.currnetNoteSubject = "全部";
          //  ConstrantManager.currentNoteSubjectID = "All";
            IsKeyPoint = -1;

            if (iv_highlight_star!=null){

                iv_highlight_star.setBackgroundResource(R.mipmap.notetool_highlight_star_unselet);

            }

            getNoteListByRefresh();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSubjectChoosedEvent(OnSubjectChoosedEvent event){

        if (event.getSubjectID()!=null && !event.getSubjectID().equals("")){

            if (event.getSubjectID().equals("All")){

                if (event.getSystemID().equals("All")){

                    mCurrentSysID = "All";

                }else {

                    mCurrentSysID = event.getSystemID();
                }

                mCurrentSubjectID =  "All";
                getNoteListByRefresh();

            }else {

                if (event.getSystemID().equals("All")){

                    mCurrentSysID = "All";

                }else {

                    mCurrentSysID = event.getSystemID();
                }

                mCurrentSubjectID = event.getSubjectID();
                getNoteListByRefresh();

            }
        }
    }

    public void getNoteListByRefresh(){

            mSwipeRefreshLayout.setRefreshing(true);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    resetParm();
                    getNoteList(ConstrantManager.currentSystemid,ConstrantManager.currentNoteSubjectID,mKeyword);

                }
            },0);

    }


    public void setKeyPoint() {

        IsKeyPoint = 1;

        getNoteListByRefresh();

    }


    public void cancelKeyPoint() {

        IsKeyPoint = -1;
        getNoteListByRefresh();
    }

    private void initSystemName() {
        switch (mCurrentSysID){
            case "630":

                mCurrentSysName = "课前预习系统";
                break;
            case "B10":

                mCurrentSysName = "课堂系统";
                break;
            case "510":

                mCurrentSysName = "课后作业系统";
                break;
            case "620":

                mCurrentSysName = "自由学习系统";
                break;
            case "S21":

                mCurrentSysName = "小管家系统";
                break;
            default:
                break;
        }

    }


    private void getNoteList(String sysID,String subjectID,String keyCon){

        pageIndex++;

        NoteInfoPostBody body = new NoteInfoPostBody();
        body.setBackUpOne("");
        body.setBackUpTwo("");
        body.setSecretKey("");
        body.setEndTime("");
        body.setStartTime("");
        body.setIsKeyPoint(IsKeyPoint);
        body.setKeycon(keyCon);
        body.setPage(pageIndex);
        body.setSize(pageSize);
        body.setSubjectID(subjectID);
        body.setSysID(sysID);
        body.setUserID(ConstrantManager.childUserID);
        body.setUserType(ConstrantManager.userType);
        body.setNoteID("");
        body.setResourceID(ConstrantManager.currentResourceID);
        body.setMaterialID(ConstrantManager.MaterialID);

        SafeApiManager.getTestApi(3).getNoteList(body)
                .compose(NetworkTransformer.<ApiResponse<NoteResponse>>commonSchedulers())
                .subscribe(new ResourceObserver<ApiResponse<NoteResponse>>() {
                    @Override
                    public void onNext(ApiResponse<NoteResponse> noteResponseApiResponse) {

                        totalCount = noteResponseApiResponse.Result.TotalCount;
                        noteList.clear();
                        noteList = noteResponseApiResponse.Result.NoteList;

                        List<NoteBean> dataList = noteList;

                        for (int i=0;i<dataList.size();i++){   //获取笔记中的图片路径

                            NoteBean noteBean = dataList.get(i);
                            noteBean.setNormal(true);//按需求需要去掉时间标签，设置notebean都是normal类型
                            if (noteBean.isNormal()){
                                String noteContent =   noteBean.getNoteContent();
                                ArrayList<String> imageUrls = HtmlUtil.getImageSrcFromText(noteContent);
                                if (imageUrls!=null && imageUrls.size()>0){
                                    noteBean.setImageUrls(imageUrls);
                                }
                            }
                        }


                        Log.e("最终拿到的数据",dataList.size()+"");

                        if (noteList!=null && noteList.size()>0){

                            currentCount = currentCount + noteList.size();
                            //暂时屏蔽
                            iv_empty.setVisibility(View.INVISIBLE);
                            tv_empty.setVisibility(View.INVISIBLE);
                            mRecyclerView.setVisibility(View.VISIBLE);

                            if (isRefreshing){  //如果处于刷新状态

                                mAdapter.removeAllItemFromDataList();
                                final Context context = mRecyclerView.getContext();
                                final LayoutAnimationController controller =
                                        AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
                                mRecyclerView.setLayoutAnimation(controller);
                                mAdapter.addAll(dataList);
                                mRecyclerView.scheduleLayoutAnimation();

                            }else {  //上拉加载更多

                                mAdapter.addAll(dataList);
                            }

                            isRefreshing = false;
                            loading = false;

                            Message message = new Message();
                            message.what = DOWNLOAD_COMPLETE;
                            mHandler.sendMessage(message);

                        }else {

                            iv_empty.setVisibility(View.VISIBLE);
                            tv_empty.setVisibility(View.VISIBLE);
                            mRecyclerView.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setRefreshing(false);
                            isRefreshing = false;

                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        mSwipeRefreshLayout.setRefreshing(false);
                        iv_empty.setVisibility(View.VISIBLE);
                        tv_empty.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                        isRefreshing = false;

                    }

                    @Override
                    public void onComplete() {

                        mSwipeRefreshLayout.setRefreshing(false);
                        isRefreshing = false;

                    }
                });

      /*  SafeApiManager.getTestApi(3).getNoteList(body)
                .compose(NetworkTransformer.<ApiResponse<NoteResponse>>commonSchedulers())
                .subscribe(new Subscriber<ApiResponse<NoteResponse>>() {
                    @Override
                    public void onCompleted() {
                        mSwipeRefreshLayout.setRefreshing(false);
                        isRefreshing = false;
                    }

                    @Override
                    public void onError(Throwable e) {

                        mSwipeRefreshLayout.setRefreshing(false);
                        iv_empty.setVisibility(View.VISIBLE);
                        tv_empty.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                        isRefreshing = false;

                    }

                    @Override
                    public void onNext(ApiResponse<NoteResponse> noteResponseApiResponse) {

                        totalCount = noteResponseApiResponse.Result.TotalCount;
                        noteList.clear();
                        noteList = noteResponseApiResponse.Result.NoteList;


                        //进行noteList的重构

                        //先获得时间列表
                        //再将时间列表中的重复项去除
                        //使用双重for循环构造新的数据列表。

                        List<NoteBean> dataList = noteList;

                        for (int i=0;i<dataList.size();i++){   //获取笔记中的图片路径

                            NoteBean noteBean = dataList.get(i);
                            noteBean.setNormal(true);//按需求需要去掉时间标签，设置notebean都是normal类型
                            if (noteBean.isNormal()){
                                String noteContent =   noteBean.getNoteContent();
                                ArrayList<String> imageUrls = HtmlUtil.getImageSrcFromText(noteContent);
                                if (imageUrls!=null && imageUrls.size()>0){
                                    noteBean.setImageUrls(imageUrls);
                                }
                            }
                        }


                        Log.e("最终拿到的数据",dataList.size()+"");

                        if (noteList!=null && noteList.size()>0){

                            currentCount = currentCount + noteList.size();
                            //暂时屏蔽
                            iv_empty.setVisibility(View.INVISIBLE);
                            tv_empty.setVisibility(View.INVISIBLE);
                            mRecyclerView.setVisibility(View.VISIBLE);

                            if (isRefreshing){  //如果处于刷新状态

                                mAdapter.removeAllItemFromDataList();
                                final Context context = mRecyclerView.getContext();
                                final LayoutAnimationController controller =
                                        AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
                                mRecyclerView.setLayoutAnimation(controller);
                                mAdapter.addAll(dataList);
                                mRecyclerView.scheduleLayoutAnimation();

                            }else {  //上拉加载更多

                                mAdapter.addAll(dataList);
                            }

                            isRefreshing = false;
                            loading = false;

                            Message message = new Message();
                            message.what = DOWNLOAD_COMPLETE;
                            mHandler.sendMessage(message);

                        }else {

                            iv_empty.setVisibility(View.VISIBLE);
                            tv_empty.setVisibility(View.VISIBLE);
                            mRecyclerView.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setRefreshing(false);
                            isRefreshing = false;

                        }

                    }
                });*/
    }


    private void deleteNote(NoteBean bean, final int positon){


        NotePostBody postBody = new NotePostBody();
        postBody.setSystemID(bean.getSystemID());
        postBody.setSystemName(bean.getSystemName());
        postBody.setUserID(ConstrantManager.childUserID);
        postBody.setUserName(ConstrantManager.currentUserName);
        postBody.setNoteContent(bean.getNoteContent());
        postBody.setNoteTitle(bean.getNoteTitle());
        postBody.setOperateFlag(3);
        postBody.setSubjectID(bean.getSubjectID());
        postBody.setSubjectName(bean.getSubjectName());
        postBody.setSchoolID(ConstrantManager.childSchoolID);
        postBody.setResourceName(bean.getResourceName());
        postBody.setResourceID(bean.getResourceID());
        postBody.setMaterialID(bean.getMaterialID());
        postBody.setMaterialIndex(bean.getMaterialIndex());
        postBody.setIsKeyPoint(bean.getIsKeyPoint());
        postBody.setNoteID(bean.getNoteID());

        SafeApiManager.getTestApi(3).addNote(postBody)
                .compose(NetworkTransformer.<OperateNoteResponse>commonSchedulers())
                .subscribe(new ResourceObserver<OperateNoteResponse>() {
                    @Override
                    public void onNext(OperateNoteResponse operateNoteResponse) {

                        if (operateNoteResponse.ErrorCode.substring(operateNoteResponse.ErrorCode.length()-2,operateNoteResponse.ErrorCode.length()).equals("00")){

                            TextViewUtils.showToast(NoteActivity.this,2,"删除成功！");

                            //局部刷新
                            if (mAdapter!=null){

                                if (mAdapter.getNoteList()!=null&&mAdapter.getNoteList().size()==1){

                                    mAdapter.removeItem(positon);//局部更新

                                    mRecyclerView.setVisibility(View.GONE);
                                    iv_empty.setVisibility(View.VISIBLE);
                                    tv_empty.setVisibility(View.VISIBLE);

                                }else {

                                    mAdapter.removeItem(positon);//局部更新

                                    mRecyclerView.setVisibility(View.VISIBLE);
                                    iv_empty.setVisibility(View.GONE);
                                    tv_empty.setVisibility(View.GONE);
                                }

                             //   mAdapter.removeItem(positon);//局部更新
                            }

                        }else {

                            TextViewUtils.showToast(NoteActivity.this,0,operateNoteResponse.Reason);

                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        TextViewUtils.showToast(NoteActivity.this,0,e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

      /*  SafeApiManager.getTestApi(3).addNote(postBody)
                .compose(NetworkTransformer.<OperateNoteResponse>commonSchedulers())
                .subscribe(new Subscriber<OperateNoteResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        TextViewUtils.showToast(NoteActivity.this,0,e.toString());

                    }

                    @Override
                    public void onNext(OperateNoteResponse operateNoteResponse) {

                        if (operateNoteResponse.ErrorCode.substring(operateNoteResponse.ErrorCode.length()-2,operateNoteResponse.ErrorCode.length()).equals("00")){

                            TextViewUtils.showToast(NoteActivity.this,2,"删除成功！");

                            //局部刷新

                            if (mAdapter!=null){

                                mAdapter.removeItem(positon);//局部更新
                            }

                        }else {

                            TextViewUtils.showToast(NoteActivity.this,0,operateNoteResponse.Reason);

                        }

                    }
                });*/
    }

    @SuppressLint("WrongViewCast")
    public void initView() {

        iv_filter = findViewById(R.id.iv_filter);
        et_search = findViewById(R.id.et_search);
        et_search.setOnClickListener(this);
        iv_filter.setOnClickListener(this);
        iv_filter.setVisibility(View.INVISIBLE);  //系统集成没有学科筛选，将入口隐藏
        iv_empty = findViewById(R.id.iv_empty);
        tv_empty = findViewById(R.id.tv_empty);
        iv_left_back = findViewById(R.id.iv_left_back);
        iv_left_back.setOnClickListener(this);


        iv_highlight_star = findViewById(R.id.iv_highlight_star);
        iv_highlight_star.setOnClickListener(this);

        iv_add_note = findViewById(R.id.iv_add_note);
        iv_add_note.setOnClickListener(this);

        fl_search = findViewById(R.id.fl_search);
        fl_search.setOnClickListener(this);

        if (ConstrantManager.currentSystemid.equals("b30")){

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500,RelativeLayout.LayoutParams.MATCH_PARENT);
            params.leftMargin = 15;
            params.topMargin = 10;
            params.bottomMargin = 10;
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            fl_search.setLayoutParams(params);

        }


        Log.e("当前选中状态",IsKeyPoint+"");
        if (IsKeyPoint == 1){

            iv_highlight_star.setBackgroundResource(R.mipmap.notetool_highlight_star);

        }else {

            iv_highlight_star.setBackgroundResource(R.mipmap.notetool_highlight_star_unselet);
        }

        mRecyclerView = findViewById(R.id.recyclerView);
        llManager = new LinearLayoutManager(NoteActivity.this);
        mRecyclerView.setLayoutManager(llManager);
        mAdapter = new NoteAdapter(NoteActivity.this,noteList);
        mAdapter.setmCurrentSysName(mCurrentSysName);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE){


                    Glide.with(NoteActivity.this).resumeRequests();

                }else {
                    //停止加载图片
                    Glide.with(NoteActivity.this).pauseRequests();

                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (dy > 0) //向下滚动
                {
                    int visibleItemCount = llManager.getChildCount();
                    int totalItemCount = llManager.getItemCount();
                    int pastVisiblesItems = llManager.findFirstVisibleItemPosition();

                    if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {  //滑动到最后一个
                        loading = true;
                        loadMoreData();
                    }
                }
            }
        });

        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshlayout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        resetParm();
                        getNoteList(ConstrantManager.currentSystemid,ConstrantManager.currentNoteSubjectID,"");

                    }
                },0);
            }
        });
    }


    private void loadMoreData(){

        if (currentCount<totalCount){ //继续加载

            mAdapter.mFooterHolder.setData(1);
            if (NetworkUtils.isConnected()){   //检查网络是否有问题

                getNoteList(ConstrantManager.currentSystemid,ConstrantManager.currentNoteSubjectID,"");

            }else {

                mAdapter.mFooterHolder.setData(3);
            }

        }else {  //显示加载完毕

            loading = false;
            mAdapter.mFooterHolder.setData(2);

        }
    }

    public void initData() {

                mSwipeRefreshLayout.setRefreshing(true);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        resetParm();
                        getNoteList(ConstrantManager.currentSystemid,ConstrantManager.currentNoteSubjectID,"");

                    }
                },0);

    }
    private void initItemListener() {
        mAdapter.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position, View itemView) {
                //点击进入编辑界面

                //    Glide.get(getActivity()).clearMemory();
                Intent intent = new Intent(NoteActivity.this, EditNoteActivity.class);
                intent.putExtra("NoteBean",mAdapter.noteList.get(position));
                startActivity(intent);

            }

            @Override
            public void onItemLongClick(final int position, View itemView) {

                PopupMenu popupMenu = new PopupMenu(NoteActivity.this,itemView);
                popupMenu.getMenuInflater().inflate(R.menu.lgnotelib_item_delete_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {

                        TextViewUtils.showDialog(NoteActivity.this, "您确定要删除该条笔记吗？"
                                , new PositiveCallBack() {
                                    @Override
                                    public void positiveCallBack(AlertDialog dialog) {

                                        deleteNote(mAdapter.noteList.get(position),position);

                                        dialog.dismiss();

                                    }
                                }, new NegativeCallBack() {
                                    @Override
                                    public void negativeCallBack(AlertDialog dialog) {

                                        dialog.dismiss();

                                    }
                                });

                        return false;
                    }
                });
                popupMenu.show();
            }
        });
    }

    private void resetParm(){

        pageSize = 10;
        pageIndex = 0;
        totalCount = 0;
        currentCount = 0;
        isRefreshing = true;

    }

    @Override
    public void onClick(View view) {

        int i = view.getId();
      if (i == R.id.iv_left_back) {
            finish();

        } else if (i == R.id.iv_filter) {

            Intent intent_filter = new Intent(NoteActivity.this, NoteSubjectActivity.class);
            startActivity(intent_filter);

        } else if (i == R.id.et_search) {

            Intent intent_search = new Intent(NoteActivity.this, SearchNoteActivity.class);
            startActivity(intent_search);

        } else if (i== R.id.iv_highlight_star){

            if (IsKeyPoint !=1){
                iv_highlight_star.setBackgroundResource(R.mipmap.notetool_highlight_star);
                setKeyPoint();
            //    IsKeyPoint = 1;


            }else {

                cancelKeyPoint();
                iv_highlight_star.setBackgroundResource(R.mipmap.notetool_highlight_star_unselet);
             //   IsKeyPoint = -1;
            }

        }else if (i == R.id.iv_add_note){

          Intent intent_subject_choose = new Intent(NoteActivity.this, AddNoteActivity.class);
          startActivity(intent_subject_choose);

        }else if (i == R.id.fl_search){

          Intent intent_search = new Intent(NoteActivity.this, SearchNoteActivity.class);
          startActivity(intent_search);

      }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);


    }
}
